package cn.hl.basic.service.impl;

import cn.hl.basic.mapper.BaseMapper;
import cn.hl.basic.query.BaseQuery;
import cn.hl.basic.service.IBaseService;
import cn.hl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * service接口的实现类
 * @param <T>
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> tMapper;

    @Override
    @Transactional
    public void add(T t) {
        tMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        tMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        tMapper.delete(id);
    }

    @Override
    public T findById(Long id) {
        return tMapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return tMapper.findAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        Long totals = tMapper.pageCount(baseQuery);
        if (totals == 0) {
            return new PageList<>();
        }
        List<T> tList = tMapper.queryData(baseQuery);
        return new PageList<>(totals, tList);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        tMapper.patchDelete(ids);
    }
}
